---
import { SITE_TITLE, THEME_LS_KEY, X_HANDLE } from '@/consts';
import type { ImageMetadata } from 'astro';

interface Props {
  title: string;
  description: string;
  image?: ImageMetadata;
}

const canonicalURL = new URL(Astro.url.pathname, Astro.site);

const {
  title,
  description,
  // fallback image
  image = {
    src: new URL('/assets/images/social.png', Astro.site),
    format: 'png',
    width: 1200,
    height: 539,
  },
} = Astro.props;
---

<!-- Global Metadata -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="icon" type="image/svg+xml" href="/favicon.ico" />
<link rel="sitemap" href="/sitemap-index.xml" />
<link
  rel="alternate"
  type="application/rss+xml"
  title={SITE_TITLE}
  href={new URL('rss.xml', Astro.site)}
/>
<meta name="generator" content={Astro.generator} />

<!-- Canonical URL -->
<link rel="canonical" href={canonicalURL} />

<!-- Primary Meta Tags -->
<title>{title}</title>
<meta name="title" content={title} />
<meta name="description" content={description} />

<meta property="twitter:card" content="summary_large_image" />
<meta property="twitter:site" content={`@${X_HANDLE}`} />
<meta property="twitter:creator" content={`@${X_HANDLE}`} />

<meta property="og:url" content={new URL(Astro.url.pathname, Astro.site)} />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:type" content="website" />
<meta property="og:image" content={image.src} />
<meta property="og:image:type" content={`image/${image.format}`} />
<meta property="og:image:width" content={String(image.width)} />
<meta property="og:image:height" content={String(image.height)} />
<script is:inline define:vars={{ THEME_LS_KEY }}>
  // Theme switcher — prevent FOUC by blocking before page loads
  if (typeof localStorage !== 'undefined') {
    const theme = localStorage.getItem(THEME_LS_KEY);
    if (theme) {
      document.documentElement.setAttribute('data-theme', theme);
    }
  }
</script>
